<?php


class adminAction extends BaseAction {
    protected $layout = 'admin';
    
    public function execute() {
        if ($_SESSION['admin'] != 'admin' && $this->params['action'] != 'login') {
            $this->redirect('admin/login');
        }
        $this->view->setLayoutPath(ROOT.'/modules/admin/layout');
        parent::execute();
    }
    
    public function actionLogin() {
        if (!empty($_POST)) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            
            if ($username == 'admin' && $password == 'admin123') {
                $_SESSION['admin'] = 'admin';
                $this->redirect('admin/index');
            }
        }
        $this->view->layout = '';
        $this->render('login');
    }
    
    public function actionLogout() {
        session_destroy();
        $this->redirect('/');
    }
    
    public function actionIndex() {
        $this->setPageTitle('欢迎');
        $this->render();
    }
    
    public function actionModule() {
        $m = camelize($this->params['m']);
        $a = camelize($this->params['a']);
        $module_class = $m.'Admin';
        
        $module = new $module_class;
        $params = $this->params;
        $params['module'] = $m;
        $params['action'] = $a;
        $module->setParams($params);
        $module->setView($this->view);
        $module->execute();
    }
}

